<template>
  <div class="register-container">
    <div class="register-box">
      <div class="register-left">
        <div class="image-wrapper">
          <img src="../assets/login/cart.jpg" alt="注册图标" />
        </div>
        <div class="left-content">
          <h3>欢迎加入</h3>
          <p>校园二手交易平台</p>
        </div>
      </div>
      <div class="register-form">
        <div class="form-header">
          <h2>创建账号</h2>
          <p class="subtitle">请填写以下信息完成注册</p>
        </div>
        <div class="form-item">
          <label>手机号码</label>
          <input type="text" v-model="username" placeholder="请输入手机号" />
        </div>
        <div class="form-item">
          <label>设置密码</label>
          <input type="password" v-model="password" placeholder="请输入密码" />
        </div>
        <div class="form-item">
          <label>确认密码</label>
          <input type="password" v-model="confirmPassword" placeholder="请确认密码" />
        </div>
        <button class="register-btn" @click="handleRegister">立即注册</button>
        <div class="form-footer">
          <span>已有账号？</span>
          <router-link to="/" class="login-link">返回登录</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { RegisterApi } from "../api/register";
const router = useRouter();
const username = ref("");
const password = ref("");
const confirmPassword = ref("");
const email = ref("");
import { ElMessage } from 'element-plus'
const handleRegister = () => {
  if (password.value !== confirmPassword.value) {
    alert("密码不一致，请重新输入");
    return; 
  }
  RegisterApi.register({
    mobile: username.value.trim(),
    password: password.value.trim(),
    confirmPassword: confirmPassword.value.trim(), 
  })
    .then((response) => {
      ElMessage.success('注册成功');
      router.push("/"); // 注册成功后跳转到登录页面 
    })

};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e9f2 100%);
  padding: 20px;
}

.register-box {
  display: flex;
  background: white;
  border-radius: 24px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  width: 1000px;
  max-width: 95%;
  position: relative;
}

.register-left {
  flex: 1;
  background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
}

.image-wrapper {
  width: 220px;
  height: 220px;
  border-radius: 20px;
  overflow: hidden;
  margin-bottom: 30px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

.register-left img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.register-left img:hover {
  transform: scale(1.05);
}

.left-content h3 {
  font-size: 24px;
  margin-bottom: 10px;
  font-weight: 600;
}

.left-content p {
  font-size: 16px;
  opacity: 0.9;
}

.register-form {
  flex: 1;
  padding: 50px;
  display: flex;
  flex-direction: column;
}

.form-header {
  margin-bottom: 40px;
}

h2 {
  color: #2E7D32;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
}

.subtitle {
  color: #666;
  font-size: 16px;
}

.form-item {
  margin-bottom: 25px;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

input {
  width: 100%;
  padding: 14px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 15px;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

input:focus {
  border-color: #4CAF50;
  background: white;
  box-shadow: 0 0 0 4px rgba(76, 175, 80, 0.1);
}

.register-btn {
  width: 100%;
  padding: 16px;
  background: #4CAF50;
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.register-btn:hover {
  background: #2E7D32;
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(46, 125, 50, 0.3);
}

.register-btn:active {
  transform: translateY(0);
}

.form-footer {
  margin-top: 30px;
  text-align: center;
  font-size: 15px;
  color: #666;
}

.login-link {
  color: #4CAF50;
  text-decoration: none;
  margin-left: 8px;
  font-weight: 500;
  transition: color 0.3s ease;
}

.login-link:hover {
  color: #2E7D32;
  text-decoration: underline;
}

@media (max-width: 768px) {
  .register-box {
    flex-direction: column;
  }

  .register-left {
    padding: 30px;
  }

  .image-wrapper {
    width: 180px;
    height: 180px;
    margin-bottom: 20px;
  }

  .register-form {
    padding: 30px;
  }
}
</style>